//
//  HPModifyMobileViewController.m
//  HPZZTong
//
//  Created by Handpay on 14-3-27.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPModifyMobileViewController.h"
#import "HPConditionTextField.h"
#import "ZZTongUtility.h"

@interface HPModifyMobileViewController ()
@property(nonatomic, retain)HPConditionTextField* mobileTextField;
@property(nonatomic, retain)HPConditionTextField* verifyCodeTextField;
@end

@implementation HPModifyMobileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"修改手机号码";
    [super viewDidLoad];
    
    
    self.modifyTableView.dataSource = self;
    
    [self.confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    
    HPConditionTextField* textField = [[HPConditionTextField alloc] initWithFrame:CGRectMake(110, 0, 200, 44)];
    textField.textColor = kNormalFontColor;
    textField.font = kNormalFont;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    switch (indexPath.row) {
        case 0:
            desLabel.text = @"新手机号码";
            textField.conditionType = HPConditionType_Numeric;
            textField.maxLength = 11;
            self.mobileTextField = textField;
            break;
        case 1:
            desLabel.text = @"短信验证码";
            textField.conditionType = HPConditionType_Numeric;
            textField.maxLength = 6;
            self.verifyCodeTextField = textField;
            
            UIButton* getVerifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
            getVerifyButton.frame = CGRectMake(215, 2, 100, 40);
            [getVerifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            UIColor* color = kVerChannel == VerChannelHeXin ? [UIColor whiteColor] : kNormalFontColor;
            [getVerifyButton setTitleColor:color forState:UIControlStateNormal];
            [getVerifyButton setBackgroundImage:[[UIImage imageNamed:@"smallbutton.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] forState:UIControlStateNormal];
            [getVerifyButton addTarget:self action:@selector(getVerifyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            getVerifyButton.titleLabel.font = kNormalFont;
            [cell addSubview:getVerifyButton];
            
            textField.frame = CGRectMake(110, 0, 105, 44);
            break;
        default:
            break;
    }
    
    [cell addSubview:desLabel];
    [desLabel release];
    
    [cell addSubview:textField];
    [textField release];
    
    return [cell autorelease];
}

- (void)getVerifyButtonClick:(UIButton*)button{
    if (![ZZTongUtility checkIsInput:self.mobileTextField tips:@"新手机号码"]) {
        return;
    }
    
    if (![ZZTongUtility checkMobile:self.mobileTextField.text tips:@"请输入正确的新手机号码"]) {
        return;
    }
    
    NSDictionary* params = @{KEY_ACTION:@"newPhone", KEY_ACCOUNT:DES_ENC(self.mobileTextField.text), KEY_CHANNEL: [ZZT_CLIENG getChannel]};
    [ZZT_CLIENG doSendVerifyCode:params withButton:button];
}

- (void)dealloc {
    self.mobileTextField = nil;
    self.verifyCodeTextField = nil;
    
    [_confirmButton release];
    [_modifyTableView release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setConfirmButton:nil];
    [self setModifyTableView:nil];
    [super viewDidUnload];
}

- (IBAction)confirmButtonClick:(id)sender {
    if (![ZZTongUtility checkIsInput:self.mobileTextField tips:@"新手机号码"]) {
        return;
    }
    
    if (![ZZTongUtility checkMobile:self.mobileTextField.text tips:@"请输入正确的新手机号码"]) {
        return;
    }
    
    if (![ZZTongUtility checkIsInput:self.verifyCodeTextField tips:@"短信验证码"]) {
        return;
    }
    
    if (![ZZTongUtility checkLength:self.verifyCodeTextField tips:@"请输入6位短信验证码" minLen:6 maxLen:6]) {
        return;
    }
    
    ZZTongClientEngine* zzt = ZZT_CLIENG;
    
    NSDictionary* params = @{KEY_ACTION:@"newPhone",
                             KEY_PHONE: DES_ENC(zzt.accountInfo.mobile),
                             @"newPhone": DES_ENC(self.mobileTextField.text),
                             KEY_VERIFYCODE:self.verifyCodeTextField.text,
                             KEY_CHANNEL: [ZZT_CLIENG getChannel],
                             @"account":DES_ENC(zzt.accountInfo.account),
                             };
    LOG(@"params:%@", params);
#if ZZT_FOR_TEST
    [self responseCallBackSuccess:NET_ACTION_MODIFY_MOBILE withData:nil];
#else
    [zzt.netEngine sendRequest:NET_ACTION_MODIFY_MOBILE withParams:params withDelegate:self];
    [zzt.uiEngine showNetWaiting];
#endif
}

- (void)doClose{
    [HPUIENG popScreenByNum:2];
}

- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    if ([action isEqualToString:NET_ACTION_MODIFY_MOBILE]) {
        ZZT_CLIENG.accountInfo.mobile = self.mobileTextField.text;
        
        AlertCommandData* commandCancel = [[AlertCommandData alloc] initCommand:@"确定" function:@selector(doClose)];
        AlertData* alertData = [[AlertData alloc] initAlertData:self title:@"提示" message:@"手机号码修改成功" commands:commandCancel, nil];
        [HPUIENG showAlertWithData:alertData];
        
        [commandCancel release];
        [alertData release];
    }
}
@end
